import * as express from 'express';
import { Request, Response } from 'express';
import * as path from 'path';
import { listFileAndPath } from '../util/FileUtil';
import { createRouterMiddleware, splitMethodAndPath } from '../util/RouterMiddlewareUtil';

const mockdir = './mock';

const api = express();

// console.log(`dirname: ${__dirname}, filename: ${__filename}`, path.resolve('mock/config.js'));

const mockpath = path.resolve(mockdir);

(function(mockdir) {

  const filenames = listFileAndPath(mockdir);
  for(const index in filenames) {
    const filename = filenames[index];
    const filepath = mockpath + path.sep + filename.substr(0, filename.lastIndexOf('.'));
    const {default: _default} = require(path.relative(__dirname, filepath));

    for(const key in _default) {
      const { method, path } = splitMethodAndPath(key, ' ');
      api.use(createRouterMiddleware(method, path, _default[key]));

    }
  }
  // const fsstats = fs.statSync(mockdir);

  // if (!fsstats.isDirectory) {
  //   throw new Error(`${mockdir} is not a directory`);
  // }

  // let dir:fs.Dir ;

  // try {
  //   dir = fs.opendirSync(mockdir);
  //   let dirent:fs.Dirent ;
  //   while(dirent = dir.readSync()) {
  //     if(dirent.isFile()) {
  //       console.log(dirent.name);
  //       const filename:string = dirent.name;
  //       const filepath = mockpath + path.sep + filename.substr(0, filename.lastIndexOf('.'));
  //       const { default: _default} = require(path.relative(__dirname, filepath));

  //       for(const key in _default) {
  //         console.log(key, _default[key]);
  //         const router:express.Router = express.Router();
  //         router.get(key, (req: Request, res: Response) => {
  //           res.json(_default[key]);
  //         });
          
  //         api.use(router);
  //       }
  //     }

      
  //   }
  // } catch(e){
  //   console.log(chalk.red(e.message));
  // } finally {
  //   if (dir) {
  //     dir.closeSync();
  //   }
  // }

})(mockpath);



// fs.stat(mockpath, (err, stat) => {
//   if (stat.isDirectory) {
//     fs.readdir(mockpath, (error, files) => {
//       if(error) {
//         console.log(error);
//       } else {
//         files.forEach((filename) => {
//           const relativeFilePath = mockdir + '/' + filename.substr(0, filename.indexOf('.'));
//           // const config = require(path.relative(__dirname, relativeFilePath));
//           import(path.relative(__dirname, relativeFilePath))
//             .then((config) => {
//               const { default:_default } = config;
//               for(const key in _default) {
//                 console.log(key, _default[key]);
//                 router.get(key, (req: Request, res: Response) => {
//                   res.json(_default[key]);
//                 });
//               }
//             });
//           // console.log(config.default);
//           // for(const key in config.default) {
//           //   console.log(key, config.default[key]);
//           //   router.get(key, (req: Request, res: Response) => {
//           //     res.json(config.default[key]);
//           //   });
//           // }
//         });
//       }
//     })
//   }
// });

api.all('/', (req: Request, res: Response) => {
  res.json({'meesage':'Hello world'});
});

export default api;